package com.kwedding.service.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kwedding.bean.TB_Brand;
import com.kwedding.bean.TB_Schedule;
import com.kwedding.dao.TB_BrandMapper;
import com.kwedding.dao.TB_ScheduleMapper;
import com.kwedding.dto.web.ResponseWebBrandSimple;
import com.kwedding.dto.web.RequestWebAddBrandSchedule;
import com.kwedding.dto.web.RequestWebGetBrandSimple;
import com.kwedding.dto.web.RequestWebGetScheduleList;
import com.kwedding.dto.web.RequestWebUpdateSchedule;
import com.kwedding.dto.web.ResponseWebSceduleList;
import com.kwedding.dto.web.ResponseWebScheduleSimple;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;

@Service
public class WebScheduleService extends LoggerParent {

	@Autowired
	private TB_ScheduleMapper tb_ScheduleMapper;
	@Autowired
	private TB_BrandMapper tb_BrandMapper;

	/**
	 * 获取该地区可用品牌
	 * 
	 * @param getBrandSimple
	 */
	@Transactional(readOnly = true)
	public List<ResponseWebBrandSimple> getBrandSimple(RequestWebGetBrandSimple getBrandSimple) {
		CheckedUtil.isNull(getBrandSimple.getCityCode(), Constants.REQUEST_ERROR_1, "地区不能为空");
		List<TB_Brand> tb_Brands = tb_BrandMapper.selectBrandList(false, null, getBrandSimple.getCityCode());
		List<ResponseWebBrandSimple> brandSimples = new ArrayList<>();
		if (tb_Brands != null && tb_Brands.size() > 0) {
			tb_Brands.stream().forEach(r -> {
				ResponseWebBrandSimple brandSimple = CopyerObject.copyerObject(r, ResponseWebBrandSimple.class);
				brandSimple.setIdStr(r.getBrandId().toString());
				brandSimples.add(brandSimple);
			});
		}
		return brandSimples;
	}

	/**
	 * 添加品牌档期
	 */
	@Transactional
	public void insertBrandSchedule(RequestWebAddBrandSchedule addBrandSchedule) {
		CheckedUtil.isNull(addBrandSchedule.getBrandId(), Constants.REQUEST_ERROR_1, "请选择品牌进行档期设置");
		CheckedUtil.isNull(addBrandSchedule.getScheduleDay(), Constants.REQUEST_ERROR_2, "档期时间不能为空");
		CheckedUtil.isNull(addBrandSchedule.getScheduleNum(), Constants.REQUEST_ERROR_3, "档期数不能为空");
		if (addBrandSchedule.getScheduleNum() < 0) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_4, "档期数不能小于0");
		}
		TB_Schedule tb_Schedule = CopyerObject.copyerObject(addBrandSchedule, TB_Schedule.class);
		tb_Schedule.setScheduleId(IDGenerate.generate());
		tb_Schedule.setStatus(false);
		tb_Schedule.setCreateTime(new Date());
		tb_Schedule.setUpdateTime(new Date());
		if (tb_ScheduleMapper.insert(tb_Schedule) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_5, "设置档期失败");
		}
	}

	/**
	 * 根据品牌或取当年每月的档期
	 * 
	 * @param getScheduleList
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ResponseWebSceduleList> getScheduleList(RequestWebGetScheduleList getScheduleList) {
		CheckedUtil.isNull(getScheduleList.getBrandId(), Constants.REQUEST_ERROR_1, "请选择要查看的品牌");
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		List<ResponseWebSceduleList> responseWebSceduleLists = new ArrayList<>();
		for (int i = 1; i < 12; i++) {
			ResponseWebSceduleList responseWebSceduleList = new ResponseWebSceduleList();
			String queryDate = null;
			if (i > 10) {
				queryDate = String.valueOf(year) + "-" + i;
			} else {
				queryDate = String.valueOf(year) + "-0" + i;
			}
			responseWebSceduleList.setDateTime(queryDate);
			List<TB_Schedule> tb_Schedules = tb_ScheduleMapper.getListByDate(getScheduleList.getBrandId(), queryDate);
			List<ResponseWebScheduleSimple> scheduleSimples = new ArrayList<>();
			if (tb_Schedules != null && tb_Schedules.size() > 0) {
				tb_Schedules.stream().forEach(r -> {
					ResponseWebScheduleSimple responseWebScheduleSimple = CopyerObject
							.copyerObject(r, ResponseWebScheduleSimple.class);
					responseWebScheduleSimple.setIdStr(r.getScheduleId().toString());
					scheduleSimples.add(responseWebScheduleSimple);
				});
			}
			responseWebSceduleList.setWebScheduleSimples(scheduleSimples);
			responseWebSceduleLists.add(responseWebSceduleList);
		}
		return responseWebSceduleLists;
	}

	/**
	 * 修改档期
	 * 
	 * @param updateSchedule
	 */
	@Transactional
	public void updateSchedule(RequestWebUpdateSchedule updateSchedule) {
		CheckedUtil.isNull(updateSchedule.getScheduleId(), Constants.REQUEST_ERROR_1, "请选择要修改的档期");
		CheckedUtil.isNull(updateSchedule.getScheduleNum(), Constants.REQUEST_ERROR_2, "剩余档期数不能为空");
		if (updateSchedule.getScheduleNum() < 0) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "档期数不能小于0");
		}
		TB_Schedule tb_Schedule = tb_ScheduleMapper.selectbyPrimaryKey(updateSchedule.getScheduleId());
		if (tb_Schedule == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_4, "修改的档期不存在或已删除");
		}
		tb_Schedule.setScheduleNum(updateSchedule.getScheduleNum());
		tb_Schedule.setUpdateTime(new Date());
		if (tb_ScheduleMapper.updateSelective(tb_Schedule) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_5, "修改档期失败");
		}
	}
}
